Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | import React, { useState, useEffect } from 'react';
import {
ShieldExclamationIcon,
UserMinusIcon,
ExclamationTriangleIcon,
ChartBarIcon,
ClockIcon,
NoSymbolIcon
} from '@heroicons/react/24/outline';
import { SecurityService } from '../../services/securityService';
import type { SecurityStatsResponse } from '../../types/security';
import SecurityIncidentsTable from './SecurityIncidentsTable';
import SecurityBansTable from './SecurityBansTable';
import SecurityStats from './SecurityStats';
import BlacklistedDevicesTable from './BlacklistedDevicesTable';
import CreateBanModal from './CreateBanModal';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import useLoadNamespace from '@/hooks/useLoadNamespace';
const SecurityPanel: React.FC = () => {
const [activeTab, setActiveTab] = useState<'stats' | 'incidents' | 'bans' | 'blacklisted-devices'>('stats');
const [stats, setStats] = useState<SecurityStatsResponse | null>(null);
const [loading, setLoading] = useState(true);
const [showCreateBanModal, setShowCreateBanModal] = useState(false);
useLoadNamespace('admin/security');
const { t } = useTranslation('admin/security');
useEffect(() => {
loadSecurityStats();
}, []);
const loadSecurityStats = async () => {
try {
setLoading(true);
const statsData = await SecurityService.getSecurityStats();
setStats(statsData);
} catch (error) {
console.error('Error loading security stats:', error);
toast.error(t('notifications.security.errorLoadingStatistics'));
} finally {
setLoading(false);
}
};
const handleCleanupExpiredBans = async () => {
try {
const result = await SecurityService.cleanupExpiredBans();
toast.success(result.message);
loadSecurityStats(); // Refresh stats
} catch (error) {
console.error('Error cleaning up expired bans:', error);
toast.error(t('notifications.security.errorCleaningExpiredBans'));
}
};
const tabs = [
{
id: 'stats' as const,
name: t('tabs.overview'),
icon: ChartBarIcon,
count: stats?.total_incidents || 0},
{
id: 'incidents' as const,
name: t('tabs.incidents'),
icon: ExclamationTriangleIcon,
count: stats?.incidents_today || 0},
{
id: 'bans' as const,
name: t('tabs.bans'),
icon: UserMinusIcon,
count: stats?.active_bans || 0},
{
id: 'blacklisted-devices' as const,
name: t('tabs.blacklistedDevices'),
icon: NoSymbolIcon,
count: 0, // We'll update this later when we get the count
},
];
if (loading) {
return (
<div className="flex items-center justify-center h-64">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary"></div>
</div>
);
}
return (
<div className="space-y-6">
{/* Header */}
<div className="bg-card shadow rounded-lg">
<div className="px-4 py-5 sm:p-6">
<div className="flex items-center justify-between">
<div className="flex items-center">
<ShieldExclamationIcon className="h-8 w-8 text-destructive mr-3" />
<div>
<h1 className="text-2xl font-bold text-foreground">{t('title')}</h1>
<p className="text-sm text-muted-foreground">
{t('subtitle')}
</p>
</div>
</div>
<div className="flex space-x-3">
<button
onClick={handleCleanupExpiredBans}
className="inline-flex items-center px-4 py-2 border border-border rounded-md shadow-sm text-sm font-medium text-muted-foreground bg-card hover:bg-muted focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary"
>
<ClockIcon className="h-4 w-4 mr-2" />
{t('actions.cleanupExpiredBans')}
</button>
<button
onClick={() => setShowCreateBanModal(true)}
className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-destructive-foreground bg-destructive hover:bg-destructive/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-destructive"
>
<NoSymbolIcon className="h-4 w-4 mr-2" />
{t('actions.createManualBan')}
</button>
</div>
</div>
</div>
</div>
{/* Quick Stats Cards */}
{stats && (
<div className="grid grid-cols-1 md:grid-cols-5 gap-6">
<div className="bg-card overflow-hidden shadow rounded-lg">
<div className="p-5">
<div className="flex items-center">
<div className="flex-shrink-0">
<ExclamationTriangleIcon className="h-6 w-6 text-yellow-400" />
</div>
<div className="ml-5 w-0 flex-1">
<dl>
<dt className="text-sm font-medium text-muted-foreground truncate">
{t('cards.totalIncidents')}
</dt>
<dd className="text-lg font-medium text-foreground">
{stats.total_incidents}
</dd>
</dl>
</div>
</div>
</div>
</div>
<div className="bg-card overflow-hidden shadow rounded-lg">
<div className="p-5">
<div className="flex items-center">
<div className="flex-shrink-0">
<ClockIcon className="h-6 w-6 text-red-400" />
</div>
<div className="ml-5 w-0 flex-1">
<dl>
<dt className="text-sm font-medium text-muted-foreground truncate">
{t('cards.todaysIncidents')}
</dt>
<dd className="text-lg font-medium text-foreground">
{stats.incidents_today}
</dd>
</dl>
</div>
</div>
</div>
</div>
<div className="bg-card overflow-hidden shadow rounded-lg">
<div className="p-5">
<div className="flex items-center">
<div className="flex-shrink-0">
<UserMinusIcon className="h-6 w-6 text-orange-400" />
</div>
<div className="ml-5 w-0 flex-1">
<dl>
<dt className="text-sm font-medium text-muted-foreground truncate">
{t('cards.activeBans')}
</dt>
<dd className="text-lg font-medium text-foreground">
{stats.active_bans}
</dd>
</dl>
</div>
</div>
</div>
</div>
<div className="bg-card overflow-hidden shadow rounded-lg">
<div className="p-5">
<div className="flex items-center">
<div className="flex-shrink-0">
<NoSymbolIcon className="h-6 w-6 text-red-600" />
</div>
<div className="ml-5 w-0 flex-1">
<dl>
<dt className="text-sm font-medium text-muted-foreground truncate">
{t('cards.permanentBans')}
</dt>
<dd className="text-lg font-medium text-foreground">
{stats.permanent_bans}
</dd>
</dl>
</div>
</div>
</div>
</div>
<div className="bg-card overflow-hidden shadow rounded-lg">
<div className="p-5">
<div className="flex items-center">
<div className="flex-shrink-0">
<svg className="h-6 w-6 text-purple-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
</svg>
</div>
<div className="ml-5 w-0 flex-1">
<dl>
<dt className="text-sm font-medium text-muted-foreground truncate">
{t('cards.blacklistedDevices')}
</dt>
<dd className="text-lg font-medium text-foreground">
--
</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
)}
{/* Tabs */}
<div className="bg-card shadow rounded-lg">
<div className="border-b border-border">
<nav className="-mb-px flex space-x-4 px-6" aria-label={t('tabs.overview')}>
{tabs.map((tab) => {
const Icon = tab.icon;
return (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id)}
className={`${
activeTab === tab.id
? 'border-primary text-primary'
: 'border-transparent text-muted-foreground hover:text-foreground hover:border-border'
} whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm flex items-center`}
>
<Icon className="h-5 w-5 mr-2" />
{tab.name}
{tab.count > 0 && (
<span className={`${
activeTab === tab.id ? 'bg-primary/10 text-primary' : 'bg-muted text-foreground'
} ml-2 py-0.5 px-2.5 rounded-full text-xs font-medium`}>
{tab.count}
</span>
)}
</button>
);
})}
</nav>
</div>
{/* Tab Content */}
<div className="p-6">
{activeTab === 'stats' && stats && <SecurityStats stats={stats} />}
{activeTab === 'incidents' && <SecurityIncidentsTable />}
{activeTab === 'bans' && <SecurityBansTable onRefresh={loadSecurityStats} />}
{activeTab === 'blacklisted-devices' && <BlacklistedDevicesTable />}
</div>
</div>
{/* Create Ban Modal */}
{showCreateBanModal && (
<CreateBanModal
isOpen={showCreateBanModal}
onClose={() => setShowCreateBanModal(false)}
onSuccess={() => {
setShowCreateBanModal(false);
loadSecurityStats();
}}
/>
)}
</div>
);
};
export default SecurityPanel;
|